package android.group.one;

import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class DataHandler extends DefaultHandler { 
	 
	  // booleans that check whether it's in a specific tag or not 	  
	  boolean question_type = false;
      boolean question_level = false;
      boolean question = false;  
      boolean questionid = false;
      boolean answer = false;
      boolean option = false;
	 
      private Vector<String> question_list = new Vector<String>();
      private Vector<String> answer_list = new Vector<String>();
      private Vector<Vector> option_list = new Vector<Vector>();
      private Vector<String> suboption;
      
      private String questionType = "";
      private String questionLevel = "";
  	
      public DataHandler(String questionType){
    	  this.questionType = questionType;
      }
      
      public Vector<String> getQuestionList(){
    	  return question_list;
      }
      public Vector<String> getAnswerList(){
    	  return answer_list;
      }
      public Vector<Vector> getOptionList(){
    	  return option_list;
      }

	  @Override 
	  public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 
		  if (qName.equalsIgnoreCase(this.questionType)) {
			  question_type = true;
          } else if (qName.equalsIgnoreCase("question_level")) {
        	  if(atts.getLength() > 0){
        		  if(atts.getValue(0).equals("level_1")){
        			  question_level = true;
        		  }
        	  }
          } else if (qName.equalsIgnoreCase("question")) {
              question = true;
              if(suboption != null){
        		  option_list.add(suboption);
        		  suboption = null;
        	  }
          } else if (qName.equalsIgnoreCase("questionid")){
        	  questionid = true;
        	  suboption  = new Vector<String>();
          } else if (qName.equalsIgnoreCase("answer")){
        	  answer = true;
          } else if (qName.equalsIgnoreCase("option")){
        	  option = true;
          }
	  } 

	  @Override 
	  public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 
		  if (qName.equalsIgnoreCase(this.questionType)) {
			  question_type = false;
          } else if (qName.equalsIgnoreCase("question_level")) {
              question_level = false;
          } else if (qName.equalsIgnoreCase("question")) {
              question = false;
          } else if (qName.equalsIgnoreCase("questionid")){
        	  questionid = false;
          } else if (qName.equalsIgnoreCase("answer")){
        	  answer = false;
          } else if (qName.equalsIgnoreCase("option")){
        	  option = false;
          }
	  } 

	  @Override 
	  public void characters(char ch[], int start, int length) { 
		  if(question && question_level && question_type){
              if(questionid){
            	  question_list.add(new String(ch, start, length).trim());
              }
              if(answer){
            	  answer_list.add(new String(ch, start, length).trim());
              }
              if(option){
            	  suboption.add(new String(ch, start, length).trim());
              }
          } 		  
	 }
} 
